/**
 * 详情对话框
 */
var DbServiceDetailInfoDlg = {
    data: {
        id: "",
        department: "",
        dbName: "",
        userId: "",
        username: "",
        password: "",
        servicetype: "",
        system: "",
        details: "",
        standard: "",
        state: "",
        spState: "",
        open: "",
        effectiveTime: "",
        remarks: "",
        revision: "",
        createdBy: "",
        createdTime: "",
        updatedBy: "",
        updatedTime: ""
    }
};


layui.use(['form', 'admin', 'ax', 'laydate', 'upload', 'formSelects', 'table', 'func', 'layer'], function () {
    var $ = layui.jquery;
    var $ax = layui.ax;
    var form = layui.form;
    var admin = layui.admin;
    var table = layui.table;
    var func = layui.func;
    var layer = layui.layer;


    //外面应该还有一个权限判断
    function getPermission() {
        $.get(Feng.ctxPath + "/databaseListKh/getUser", function (obj) {
            // console.log(obj)
            // console.log( Feng.getUrlParam("id"))
            if (obj.data.deptId != 25) {
                //判断当前数据库是否是登录账号所申请的数据库
                if (obj.data.id != Feng.getUrlParam("userId")) {
                    //获取点击详情的数据库状态
                    $.get(Feng.ctxPath + "/interfaceapproval/list?serviceid=" + Feng.getUrlParam("id"), function (obj) {
                        // console.log(obj.data)
                        if (obj.data.length == 0) {
                            if (obj.data == null || obj.data == "") {
                                $("#stateButton").empty();
                                $("#stateButton").append("<button class='layui-btn layui-btn-normal layui-btn-sm' id='apply' type='button'>申请使用</button>")
                            }
                        } else {
                            if (obj.data != null || obj.data != "") {
                                if (obj.data[0].state == 0) {
                                    $("#stateButton").empty();
                                    $("#stateButton").append("<span>已申请</span>")
                                }
                                if (obj.data[0].state == 1) {
                                    $("#stateButton").empty();
                                    $("#stateButton").append("<span>已批准</span>")
                                }
                                if (obj.data[0].state == 2) {
                                    $("#stateButton").empty();
                                    $("#stateButton").append("<span>已拒绝</span>")
                                }
                                if (obj.data[0].state == 3) {
                                    $("#stateButton").empty();
                                    $("#stateButton").append("<span>已完成</span>")
                                }
                            }
                        }
                    })
                } else {
                    $("#stateButton").empty();
                    $("#stateButton").append("<span>本部门数据库</span>")
                }
            } else {
                //获取点击详情的数据库状态
                $.get(Feng.ctxPath + "/interfaceapproval/list?serviceid=" + Feng.getUrlParam("id"), function (obj) {
                    $("#stateButton").empty();
                })
            }
        })
    }


    $(function () {
        // getDetailState();
        getPermission();
    })


    //如果是可申请状态,点击跳转申请界面
    $("#stateButton").on('click', '#apply', function () {
        //获取申请前的数据库id
        var id = $("#id").val();
        console.log(id)
        layer.open({
            title: '',
            area: ['80%', '80%'],
            content: Feng.ctxPath + "/dbContentapproval/edit?id=" + id,
            type: 2,
            end: function () {
                getPermission();
            }
            // area: ['1500px', '600px'],
            // end:function () {
            //
            // }
        })
    })

    //获取详情信息，填充表单
    var ajax = new $ax(Feng.ctxPath + "/databaseListKh/detail?id=" + Feng.getUrlParam("id"));
    var result = ajax.start();
    // console.log(result.data);
    var Obj = result.data;
    if (Obj.servicetype == 1) {
        Obj.servicetype = "数据库服务"
    }
    form.val('dbServicelistForm', result.data);


    $("#returnDiv").on('click', '#returnButton', function () {
        // parent.location.reload()
        history.go(-1)
    })


    /**
     * 服务详情管理
     */
    var Servicedetails = {
        tableId: "servicedetailsTable"
    };

    /**
     * 初始化表格的列
     */
    Servicedetails.initColumn = function () {
        return [[
            {field: 'id', hide: true, title: 'id'},
            {
                field: 'rank',
                title: '序号',
                width: 100,
                align: 'center',
                templet: '#rank',
                style: 'background-color:#ffffff'
            },
            {field: 'serviceId', hide: true, title: '服务id'},
            {field: 'tableName', title: '表名称', width: 200, align: 'center', style: 'background-color:#ffffff'},
            {field: 'details', title: '说明', align: 'left', style: 'background-color:#ffffff'},
            {field: 'revision', hide: true, title: '乐观锁'},
            {field: 'createdBy', hide: true, title: '创建人'},
            {field: 'createdTime', hide: true, title: '创建时间'},
            {field: 'updatedBy', hide: true, title: '更新人'},
            {field: 'updatedTime', hide: true, title: '更新时间'},
        ]];
    };


    // 渲染表格
    var tableResult = table.render({
        elem: '#' + Servicedetails.tableId,
        url: Feng.ctxPath + '/servicedetails/list?serviceId=' + Feng.getUrlParam("id"),
        page: true,
        height: "full-158",
        cellMinWidth: 100,
        cols: Servicedetails.initColumn(),
        skin: 'line',
        done: function () {
            $('tr').css('background-color', '#ffffff')
        }
    });


    /**
     * 文本域高度自适应
     */
    //jQuery实现textarea高度根据内容自适应
    $.fn.extend({
        txtaAutoHeight: function () {
            return this.each(function () {
                var $this = $(this);
                if (!$this.attr('initAttrH')) {
                }
                setAutoHeight(this).on('input', function () {
                    setAutoHeight(this);
                });
            });

            function setAutoHeight(elem) {
                var $obj = $(elem);
                return $obj.css({height: $obj.attr('initAttrH'), 'overflow-y': 'hidden'}).height(elem.scrollHeight);
            }
        }
    });
    //调用
    $(function () {
        $("#details").txtaAutoHeight();
        $("#standard").txtaAutoHeight();
    });
});